package com.teame.readbaby;

import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.teame.readbaby.parser.FavProductParser;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.util.GetImageUtil;
import com.teame.readbaby.vo.FavProduct;

public class FavouritesActivity extends BaseActivity {
	private ListView myfavorite_product_list;
	private MBaseAdapter mBaseAdapter;
	private TextView head_back_text; //返回按钮

	private List<FavProduct> favProductList;

	@Override
	protected void onClickEvent(View paramView) {
		if(paramView==head_back_text){
			Intent intent=new Intent(context,MoreActivity.class);
			startActivity(intent);
			finish();
		}
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.my_favorite_activity);

	}

	@Override
	protected void findViewById() {
		myfavorite_product_list = (ListView) findViewById(R.id.myfavorite_product_list);
		head_back_text=(TextView) findViewById(R.id.head_back_text);
	}

	@Override
	protected void processLogic() {
		String url = ConstantValue.HOST + ConstantValue.FAVORITES;
		this.getDataFromServer(url, null, new FavProductParser(),
				new DataCallback<List<FavProduct>>() {
					@Override
					public void processData(List<FavProduct> favProductList,
							boolean paramBoolean) {
						FavouritesActivity.this.favProductList = favProductList;
						mBaseAdapter = new MBaseAdapter();
						myfavorite_product_list.setAdapter(mBaseAdapter);
					}

				});
	}

	@Override
	protected void setListener() {
		head_back_text.setOnClickListener(this);
	}

	class MBaseAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return favProductList.size();
		}

		@Override
		public Object getItem(int position) {
			return favProductList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder =null;
			if(convertView==null){
				holder = new ViewHolder();
				convertView=View.inflate(context, R.layout.my_favorite_listitem, null);
				holder.myfavorite_title_text=(TextView) convertView.findViewById(R.id.myfavorite_title_text);
				holder.myfavorite_price_text=(TextView) convertView.findViewById(R.id.myfavorite_price_text);
				holder.myfavorite_deleteprice_text=(TextView) convertView.findViewById(R.id.myfavorite_deleteprice_text);
				holder.myfavorite_nostock_text=(TextView) convertView.findViewById(R.id.myfavorite_nostock_text);
				holder.myfavorite_product_img= (ImageView)convertView.findViewById(R.id.myfavorite_product_img);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			
			FavProduct favProduct = favProductList.get(position);
			holder.myfavorite_title_text.setText(favProduct.getName());
			holder.myfavorite_price_text.setText(favProduct.getPrice()+"");
			holder.myfavorite_deleteprice_text.setText(favProduct.getMarketprice()+"");
			holder.myfavorite_nostock_text.setText(100+"");//假的,因为数据库里没有评论
			String replacedStr=favProduct.getPic();
			//FIXME 4月4号晚上测试用
			Bitmap bitmap = GetImageUtil.getBitmap(replacedStr);
			System.out.println(bitmap);
			
			holder.myfavorite_product_img.setImageBitmap(bitmap);
			//holder.myfavorite_product_img.setImageURI(Uri.parse(replacedStr));
			
			Paint paint = holder.myfavorite_deleteprice_text.getPaint();
			paint.setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
			paint.setAntiAlias(true);
			
			return convertView;
		}

	}

	static class ViewHolder {
		TextView myfavorite_title_text; // 产品标题
		TextView myfavorite_price_text; // 产品价格
		TextView myfavorite_deleteprice_text; // 产品市场价/原价
		TextView myfavorite_nostock_text; // 产品这是评论数目
		ImageView myfavorite_product_img; // 这是产品的缩略图片

	}

}
